package com.yjd.bi.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yjd.bi.base.service.impl.BaseServiceImpl;
import com.yjd.bi.report.enums.TimeType;
import com.yjd.bi.report.model.EChartsModel;
import com.yjd.bi.report.util.TimeDimensionUtil;
import com.yjd.bi.user.dao.IUserStatisticsDao;
import com.yjd.bi.user.model.UserStatisticsModel;
import com.yjd.bi.user.service.IUserStatisticsService;
import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.util.ArithUtil;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.NumberUtil;

/**
 * <b>description</b>：客户消息订单业务实现<br>
 * <b>time</b>：2016-04-13 09:08:34 <br>
 * <b>author</b>： zhoujin
 */
@Service("userStatisticsService")
public class UserStatisticsServiceImpl extends BaseServiceImpl implements
		IUserStatisticsService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public UserStatisticsModel insert(UserStatisticsModel model)
			throws Exception {
		return this.userStatisticsDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(UserStatisticsModel model) throws Exception {
		return this.userStatisticsDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.userStatisticsDao.deleteModel(paramMap, true);
	}

	@Override
	public Integer getSummary(Map<String, Object> paramMap)
			throws Exception {
		return userStatisticsDao.getSummary(paramMap);
	}

	@SuppressWarnings("unchecked")
	@Override
	public EChartsModel getDates(TimeType timeType, Long timeFrom, String field)
			throws Exception {
		EChartsModel ec = new EChartsModel();
		ec.setxAxis(FrameUtil.newArrayList(null));
		ec.setLegend(FrameUtil.newArrayList(getFormateX(timeFrom)));
		ec.addSeries(FrameUtil.newArrayList(null), getFormateX(timeFrom));
		List<List<Long>> searchParam = TimeDimensionUtil.getSearchRanges(
				timeType, timeFrom);
		if(timeType==TimeType.DAY){
			for (int i = searchParam.size() - 1; i >= 0; i--) {
				ec.getXAxis().add(DateUtil.getFormateStringByTimeDimention(searchParam.get(i).get(1), "yyyy-MM-dd"));
				Double sum = ArithUtil.round(this.userStatisticsDao.getFundsSum(
						FrameUtil.newHashMap("field", field, "start", searchParam
								.get(i).get(0), "end", searchParam.get(i).get(1)),
						false), 2);
				ec.getSeries().get(0).getData()
						.add(NumberUtil.formatNumber(sum, NumberUtil.FORMAT_1));
			}
		}else{
			for (int i = searchParam.size() - 1; i >= 0; i--) {
				ec.getXAxis().add(DateUtil.getFormateStringByTimeDimention(searchParam.get(i).get(1), "yyyy-MM-dd"));
				// 注册用户数
				Integer sum=this.userStatisticsDao.getSummary(FrameUtil.newHashMap( "start",DateUtil.dateStringToTimestamp(searchParam
						.get(i).get(0).toString(), DateUtil.PATTERN_yyyyMMdd), "end",DateUtil.dateStringToTimestamp( searchParam.get(i).get(1).toString(), DateUtil.PATTERN_yyyyMMdd)+86400,"propertyName", field));
				ec.getSeries().get(0).getData()
				.add( sum.toString() );
			}
		}
		return ec;
	}


    @Override
    public Integer wxBindCount(Map<String, Object> paramMap) throws Exception {
        return this.userStatisticsDao.wxBindCount(paramMap);
    }

    public String getFormateX(Long value) {
		return DateUtil.timestampToDateString(DateUtil.dateStringToTimestamp(
				value + "", DateUtil.PATTERN_yyyyMMdd),
				DateUtil.PATTERN_yyyy_MM_dd);
	}

	@Resource
	private IUserStatisticsDao userStatisticsDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.userStatisticsDao;
	}

}
